package leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * 描述:给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度
 *
 * @author Madison You
 * @created 21:11
 */
public class Mid_3_LengthOfLongestSubstring {

    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abba"));
    }

    public static int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>();
        int max = 0, start = 0;
        for (int end = 0; end < s.length(); end++) {
            char ch = s.charAt(end);
            if (map.containsKey(ch)) {
                start = Math.max(map.get(ch) + 1, start);
            }
            max = Math.max(max, end - start + 1);
            map.put(ch, end);
        }
        return max;
    }

    public static int lengthOfLongestSubstring1(String s) {
        int maxlength = 0;
        String str = "";
        String[] split = s.split("");
        int begin = 0;
        int index = 0;
        while (begin < split.length) {
            if (str.indexOf(split[begin]) != -1) {
                int i = str.indexOf(split[begin]);
                index = index + i + 1;
                begin = index;
                maxlength = Math.max(maxlength, str.length());
                str = "";
            } else {
                str += split[begin];
                begin++;
            }
        }
        return Math.max(maxlength, str.length());
    }

}
